<?php
class ProductModule
{
	function index(){
		global $_G;
		$lang = lang::load('user');
		$limit = 15;
		$links = array();
		$id = getGP('id','G','int');
		$where .= '';
		if($id) {
			$where .= ' AND l.cid='.$id;
		}
		if(getGP('special','G','int')){
			$where .=' AND m.special=1';	
		}
		$sql = 'SELECT m.* FROM'.table('product').'m WHERE m.uid='.$_G['uid'].$where;
		$query = $_G['db']->page_query($sql,$limit);
		$num = $_G['db']->result('SELECT m.*,c.catename FROM'.table('product').'m LEFT JOIN'.table('category').'c ON m.cid=c.cid WHERE c.type=1 AND m.uid='.$_G['uid'].$where);
		while($data = $_G['db']->fetch_array($query)) {
			$datas[] = $data ;
		}
		$query = $_G['db']->query("SELECT cid,catename FROM".table('category')."WHERE uid='$_G[uid]' AND type=1 " );
		while($d = $_G['db']->fetch_array($query)){
			$categorys[$d['cid']]=$d;
		}
		$pager = pager($num,$limit);
		include template('uc/product');
	}

	function add(){
		global $_G;
		$lang = lang::load('user');
		$categorys = $_G['db']->fetch_all("SELECT cid,catename FROM".table('category')."WHERE uid='$_G[uid]' AND type=1 " );
		include template('uc/product');
	}
	
	function edit(){
		global $_G;
		$lang = lang::load('user');
		$data = $_G['db']->fetch('product','*',array('pid'=>getGP('id','G','int'),'shopid'=>$_G['users']['shopid']));
		$categorys = $_G['db']->fetch_all("SELECT cid,catename FROM".table('category')."WHERE uid='$_G[uid]' AND type=1 " );
		include template('uc/product');
	}
	
	function del(){
		global $_G;
		$id = getGP('id','G','int');
		if($id){
			$_G['db']->delete('product',array('uid'=>$_G['uid'],'shopid'=>$_G['users']['shopid'],'pid'=>$id));
		}
		msg('操作成功');
	}
	
	function save(){
		global $_G;
		if(!IS_POST || $_G['users']['shopid']<1) {
			msg('禁止操作');
		}
		if(strlen(getGP('title','P'))<1){
			msg("请输入名称");
		}
		$pid = getGP('pid','P','int');
		$result = array(
			'stock' => getGP('stock','P','int'),
			'uid'	=> $_G['uid'],
			'cid' => getGP('cid','P','int'),
			'status' => getGP('status','P','int'),
			'price' => getGP('price','P'),
			'recommend' => getGP('special','P') == 'on'?1:0,
			'content' => getGP('detail','P'),
			'title' => getGP('title','P'),
			'shopid' => $_G['users']['shopid'],
		) ;
		if(isset($_FILES['image']) && $_FILES['image']['size']>1) {
			include libfile('upload.php');
			$upload = new upload($_G['settings']['upload']['allow_type'], $_G['settings']['upload']['file_size'], 'image');
			$upload->set_dir(BASE_ROOT.DATA_DIR.'files/');
			//对图像进行缩放
			$upload->set_resize(110, 135);
			$files = $upload->execute_single('u'.$_G['uid']);
			if($files['status'] == 1){
				$result['image'] = str_replace(DATA_DIR.'files/', '', $files['path'].$files['name']);
			}
		}
		if($pid>0){
			$result['updated'] = TIMESTAMP;
			$_G['db']->update('product',$result,array('uid'=>$_G['uid'],'pid'=>$pid));
			msg('编辑成功',url('home.php?m=product'));
		}else{
			$result['created'] = TIMESTAMP;
			$_G['db']->insert('product',$result);
			msg('添加成功',url('home.php?m=product'));
		}
	}
	
	function cate(){
		global $_G;
		$links = array();
		$lang = lang::load('user');
		loadialog();
		$datas = $_G['db']->fetch_all("SELECT cid,catename,total FROM".table('category')."WHERE uid='$_G[uid]' AND type=1 " );
		include template('uc/product');
	}
	
	function savecate(){
		global $_G;
		if($_G['uid']<1){
			exit('0');
		}
		$lang = lang::load('user');
		$cates['uid'] = $_G['uid'];
		$cates['catename'] = getGP('name','G');
		$cates['status'] = getGP('cstatus','G','int');
		$cates['cid'] = getGP('id','G','int');
		$cates['type'] = 1;
		
		if($cates['cid']<1){
			$exist = $_G['db']->result("SELECT cid FROM ".table('category')." WHERE (uid='".$cates['uid']."' OR uid=0) AND type=1 AND catename='".$cates['catename']."'");
			
			if($exist){
				$cates['mstatus'] = false;
				$cates['msg'] = $lang['category_exists'];
				
			}else{
				$cates['cid'] = $_G['db']->insert("category",$cates);
				if($cates['cid']){
					$cates['mstatus'] = true;
				}
			}
		}else{
			$exist = $_G['db']->result("SELECT cid FROM ".table('category')." WHERE (uid='".$cates['uid']."' OR uid=0) AND type=1 AND catename='".$cates['catename']."' AND cid!='".$cates['cid']."'");
			if($exist){
				$cates['mstatus'] = false;
				$cates['msg'] = $lang['category_exists'];
				
			}else{
				$_G['db']->update("category",$cates,array('cid'=>$cates['cid'],'uid'=>$_G['uid'] ));
				$cates['mstatus'] = true;
			}
		}
		if(!$cates['mstatus'])$cates['mstatus'] = false;
		
		json($cates);
	}
}